package com.welkom.everydate.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.welkom.everydate.domain.Menu;
import com.welkom.everydate.domain.Resource;

@Repository
public class ResourceDao extends BaseDao {
	public static final String GET_RESOURCES_BY_AUTHORITY_ID="getResourcesByAuthorityId";
	public static final String GET_ALL_RESOURCES="getAllResources";
	public static final String GET_ALL_RESOURCES_BY_PAGE="getResourcesByPage";
	public static final String GET_RESOURCE_NUMBER="getReourceTotalCount";
	public static final String GET_RESOURCE_BY_ID="getResourceById";
	public static final String UPDATE_RESOURCE="updateResource";
	public static final String REMOVE_RESOURCE_BY_ID="removeResourceById";
	public static final String ADD_RESOURCE="addResource";
	public static final String REMOVE_AUTHORITY_RESOURCE_RELATIONS_BY_AUTHORITY_ID="removeAuthorityResourceRelationsByAuthorityId";
	public static final String ADD_AUTHORITY_RESOURCE_RELATION="addAuthorityResourceRelation";
	public static final String GET_RESOURCES_BY_AUTHORITY_IDS="getResourcesByAuthorityIds";
	public List<Resource> getResourcesByAuthorityId(long authorityId){
		Map<String, Object> parameters=new HashMap<String,Object>();
		parameters.put("authority_id", authorityId);
		return (List<Resource>) getSqlMapClientTemplate().queryForList(GET_RESOURCES_BY_AUTHORITY_ID, parameters);
	}
	
	public List<Resource> getAllResourcesByPage(int startIndex,int limit){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("start_index", startIndex);
		parameters.put("limit", limit);
		return (List<Resource>)getSqlMapClientTemplate().queryForList(GET_ALL_RESOURCES_BY_PAGE, parameters);
	}
	
	public int getReourceTotalCount(){
		return (Integer)getSqlMapClientTemplate().queryForObject(GET_RESOURCE_NUMBER);
	}
	
	public Resource getResourceById(long resourceId){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("resource_id", resourceId);
		return (Resource)getSqlMapClientTemplate().queryForObject(GET_RESOURCE_BY_ID,parameters);
	}
	
	public void updateResource(Resource resource){
		getSqlMapClientTemplate().update(UPDATE_RESOURCE,resource);
	}
	
	public void addResource(Resource resource){
		getSqlMapClientTemplate().insert(ADD_RESOURCE, resource);
	}
	
	public void removeResourceById(long resourceId){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("resource_id", resourceId);
		getSqlMapClientTemplate().delete(REMOVE_RESOURCE_BY_ID, parameters);
	}
	
	public List<Resource> getAllResources(){
		return (List<Resource>)getSqlMapClientTemplate().queryForList(GET_ALL_RESOURCES);
	}
	
	public void removeAuthorityResourceRelationsByAuthorityId(long authorityId){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("authority_id", authorityId);
		getSqlMapClientTemplate().delete(REMOVE_AUTHORITY_RESOURCE_RELATIONS_BY_AUTHORITY_ID, parameters);
	}
	
	public void addAuthorityResourceRelations(List<Resource> resources,long authorityId){
		for(Resource resource:resources){
			Map<String,Object> parameters=new HashMap<String,Object>();
			parameters.put("authority_id", authorityId);
			parameters.put("resource_id", resource.getResourceId());
			getSqlMapClientTemplate().insert(ADD_AUTHORITY_RESOURCE_RELATION, parameters);
		}
	}
	
	public List<Resource> getResourcesByAuthorityIds(List<Long> authorityIds){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("authorityIds", authorityIds);
		return (List<Resource>)getSqlMapClientTemplate().queryForList(GET_RESOURCES_BY_AUTHORITY_IDS, parameters);
	}
	
	
}
